package com.icbci.process.batch;

import com.icbci.process.pojo.Process;
import com.icbci.process.service.ProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class BatchSchedule {

    @Autowired
    private ProcessService processService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Scheduled(cron = "0/5 * * * * *")
    public void run()  {

        List<Process> list = new ArrayList<>();

        for (int i = 0; i <2000 ; i++) {
            Process process = (Process) redisTemplate.opsForList().rightPop("test");
            if(process!=null){
                list.add(process);
            }else {
                break;
            }
        }

        if (list.size()!=0){

            processService.save(list);

        }
    }

}
